from django.db import models

# Create your models here.
from django.db import models
from django_celery_beat.models import PeriodicTask, IntervalSchedule
import json
from django.db import models
from django.utils.timezone import now

#城市模型
class City(models.Model):
    name = models.CharField(max_length=100, unique=True)
    code = models.CharField(max_length=50, unique=True)
    description = models.TextField(blank=True)

    class Meta:
        verbose_name_plural = "Cities"

    def __str__(self):
        return self.name

#机房
class DataCenter(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=50, unique=True)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    address = models.TextField(blank=True)

    def __str__(self):
        return f"{self.name} ({self.city})"

#主机
class Host(models.Model):
    STATUS_CHOICES = [
        ('active', 'Active'),
        ('maintenance', 'Maintenance'),
        ('retired', 'Retired'),
    ]

    hostname = models.CharField(max_length=100, unique=True)
    ip_address = models.GenericIPAddressField(unique=True)
    data_center = models.ForeignKey(DataCenter, on_delete=models.CASCADE)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.hostname} ({self.ip_address})"

#主机密码
class HostPassword(models.Model):
    host = models.OneToOneField(Host, on_delete=models.CASCADE, related_name='password_info')
    root_password = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now=True)
    history = models.JSONField(default=list)  # 存储密码修改历史

    def __str__(self):
        return f"Password for {self.host}"


#根据城市数据统计
class CityHostStatistics(models.Model):
    """城市主机统计数据"""
    city = models.ForeignKey('City', on_delete=models.CASCADE, related_name='host_stats')
    date = models.DateField(default=now().date)
    host_count = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('city', 'date')
        ordering = ['-date', 'city__name']

    def __str__(self):
        return f"{self.city.name} - {self.date}: {self.host_count} hosts"

#根据机房数据统计
class IDCHostStatistics(models.Model):
    """机房主机统计数据"""
    idc = models.ForeignKey('DataCenter', on_delete=models.CASCADE, related_name='host_stats')
    date = models.DateField(default=now().date)
    host_count = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('idc', 'date')
        ordering = ['-date', 'idc__city__name', 'idc__name']

    def __str__(self):
        return f"{self.idc.name} ({self.idc.city.name}) - {self.date}: {self.host_count} hosts"